<?php


namespace app\system\service;

class PublicFileService
{
	public static function getlist($options)
	{
		$where = [];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["type"]) && $options["type"] != "") {
			$where[] = ["type", "=", $options["type"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%{$options["name"]}%"];
		}
		$list = \think\Db::name("public_upload_log")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("public_upload_log")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		if (empty($item["channel"])) {
			if ($item["type"] == 0) {
				$item["url"] = get_static_resources_url($item["uri"]);
			} elseif ($item["type"] == 1) {
				$item["url"] = get_static_resources_url($item["uri"], 1);
			} elseif ($item["type"] == 2) {
				$item["url"] = get_static_resources_url($item["uri"], 2);
			}
		}
		return $item;
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("public_upload_log")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			if (file_exists($row["uri"])) {
				unlink($row["uri"]);
			}
			\think\Db::name("public_upload_log")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function batch_del_expire($options)
	{
		\think\Db::startTrans();
		try {
			$list = \think\Db::name("public_upload_log")->where([["overtime", "<>", 0], ["overtime", "<", time()]])->select();
			foreach ($list as $row) {
				if (file_exists($row["uri"])) {
					unlink($row["uri"]);
				}
			}
			\think\Db::name("public_upload_log")->where([["overtime", "<>", 0], ["overtime", "<", time()]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success, "msg" => "成功删除 " . count($list) . " 个过期文件"];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function upload_file($options)
	{
		$ip = \think\facade\Request::ip();
		$save_dir = "uploads/public_file";
		try {
			$file = request()->file("file");
			if (!$file) {
				throw new \Exception("请传入文件");
			}
			$info = $file->move($save_dir);
			if (empty($info)) {
				throw new \Exception($file->getError());
			}
			$data = array_merge(["ip" => $ip, "name" => $file->getInfo("name"), "type" => \app\common\model\File::type_image, "addtime" => time(), "overtime" => 0, "source" => 1], handle_file_storage($file, $info, $save_dir));
			$file_id = \think\Db::name("public_upload_log")->insertGetId($data);
			return ["code" => \app\system\model\Code::success, "msg" => "上传成功", "data" => self::get_array_info(\think\Db::name("public_upload_log")->where(["id" => $file_id])->find())];
		} catch (\Exception $e) {
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}